//直接在里面，默认页面一打开就会执行
let ipt_account=document.getElementById("ipt_account");
let ipt_password=document.getElementById("ipt_password");
let cb_rem=document.getElementById("cb_rem");
//页面一打开就从localStorage中去寻找登录的数组
let users=JSON.parse(localStorage.getItem("users"));
//记住密码的第二步
let rem_user=localStorage.getItem("rem_user");
if(rem_user){
	rem_user=JSON.parse(rem_user);
	ipt_account.value=rem_user.account;
	ipt_password.value=rem_user.password;
	cb_rem.checked=true;
}
/**
 * 登录方法
 */
function login(){
	let account=ipt_account.value;
	let password=ipt_password.value;
	let rem=cb_rem.checked;
	
	if(account.trim().length===0){
		alert("请输入账号");
		return;
	}
	if(password.length<6){
		alert("密码至少要求6位");
		return;
	}
	let result=false;
	for (let index in users) {
		item=users[index];
		if (item!==null) {
			if (item.account===account&&item.password===password) {
				result=true;
		}
	}
}
if(result){
	//登录成功
		//是否需要记住密码
		if (rem) {
			let rem_user={
				account:account,
				password:password
			}
			localStorage.setItem("rem_user",JSON.stringify(rem_user));
		}else{
			localStorage.setItem("rem_user",null);
		}
		//将用户账号存到sessionStorage中
		sessionStorage.setItem("account", account);
		location.href="index.html";
		
	}else{
		//登录失败
		alert("账号或者密码错误");
	}
}

function reg(){
	location.href="reg.html";
}